var ns, FNScanner, UIActionProgress;
var app = new Vue({
    el: '#app',
    data: {
        TotalCost: 0, //今日营业总额
        WaitTransOrder: 0, //待发货订单数
        WaitReceiveOrder: 0, //待收货订单数
        WaitRebackOrder: 0, //退货申请数
        UserInfo:{},
        businessinfo: {},
        // BusinessStatus: '营业中',
        domainURL: CONFIG.url,
    },
    methods: {
        get_data: function() {
            var that = this;
            var option = {};
            option.action = "getbusinessindexinfo";
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.businessinfo = data.backInfo.BusinessInfo;
                    that.UserInfo=data.backInfo.UserInfo;
                    that.TotalCost=data.backInfo.TotalCost;
                    that.WaitTransOrder=data.backInfo.WaitTransOrder;
                    that.WaitReceiveOrder=data.backInfo.WaitReceiveOrder;
                    that.WaitRebackOrder=data.backInfo.WaitRebackOrder;
                    //alert(JSON.stringify(data));
                    // that.businessinfo = data.businessinfo;
                    // that.count = data.count;
                    // that.TotalAmount = data.TotalAmount;
                    // that.RefundAmount = data.RefundAmount;

                    //alert(that.domainURL+that.businessinfo.CoverImage);
                    //alert(that.businessinfo.BusinessHoursStart);
                    //alert(that.businessinfo.BusinessHoursEnd);
                    // alert(formatDateTime1(new Date(),that.businessinfo.BusinessHoursStart));
                    // alert(formatDateTime1(new Date(),that.businessinfo.BusinessHoursEnd));
                    // alert(formatDateTime(new Date()));
                    // if (nowInDateBetwen(formatDateTime1(new Date(), that.businessinfo.BusinessHoursStart), formatDateTime1(new Date(), that.businessinfo.BusinessHoursEnd))) {
                    //     that.BusinessStatus = "营业中";
                    // } else {
                    //     that.BusinessStatus = "已打样";
                    // }
                }
            });
        },
        // login:function(){
        //     var that=this;
        //     ns.openWin('login_frm', '登录');
        // },
        open_orderlist:function(){
            var that=this;
            ns.openWin('home_orderlist_frm', '订单中心',{
                headerbg:1,
                searchtype: 26
            });

        },
        // 消息列表
        do_openNews:function(){
            ns.openWin('xiaoxi_list', '消息列表');
        },
        open_shop:function(){
            var that=this;
            ns.openWin('shopmsg_frm', '商家信息');
        },
        open_productlist: function(item) {
            var that = this;
            ns.openWin('productlist', '商品管理', {
                headerbg:1,
                searchtype: 27
            });
        },
        //交易流水
        open_analysis:function(){
            var that = this;
            ns.openWin('analysis_historyfeelist_frm', '交易流水',{
                searchtype:28,
            });
        },
        // 库存
        open_kucunlist:function(){
            var that = this;
            ns.openWin('kucun_list', '库存管理');
        },
        // 退款订单
        open_backorder:function(){
            var that = this;
            ns.openWin('back_orderlist', '退款订单',{
                headerbg:1,
            });
        },
        //营销活动
        open_activity:function(){
            var that = this;
            ns.openWin('activity_list', '营销活动',{
                headerbg:1,
                searchtype:29
            });
        },
        //打开经营看板
        open_kanban:function(){
            var that = this;
            ns.openWin('kanban_frm', '经营看板',{
                headerbg:2,
            });
        },
        // 系统设置
        open_xitongset:function(){
            var that = this;
            ns.openWin('xt_setting', '系统设置');
        },
        // 账号权限
        open_zhanghao:function(){
            var that = this;
            ns.openWin('dl_zhanghulist', '账户权限');
        },

        // 打开vip
        open_vip:function(){
            var that = this;
            ns.openWin('vip_productlist', '会员中心',{
                headerbg:1,
            });
        },
        //优惠卡券
        open_youhuiCard:function(){
            var that = this;
            ns.openWin('youhui_cardlist', '优惠卡券',{
                headerbg:1,
                searchtype:31
            });
        },



        open_shourutj: function(item) {
            var that = this;
            ns.openWin('dl_shourutj_frm', '收入统计', {
                id: 0,
            });
        },
        open_jiesuan: function(item) {
            var that = this;
            ns.openWin('dl_jiesuan_frm', '结算中心', {
                id: 0,
            });
        },


        open_login: function(item) {
            var that = this;
            ns.openWin('login_frm', '', {
                id: 0,
            });
        },


        closeWin: function() {
            var that = this;
            //setTimeout(function() {
            // api.closeFrame();
            //back();
            //}, 500);
        },
        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    app.set_status_bar();
    ns = window.Foresight.Util;
    FNScanner = api.require('FNScanner');
    UIActionProgress = api.require('UIActionProgress');
    ns.init_pull();
    
    app.get_data();
    api.addEventListener({
        name: 'onlogin'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'shopmsgevent'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'saveorder'
    }, function(ret, err) {
        app.get_data();
    });
    setTimeout(function() {
        appupgrade.check_upgrade(false);
    }, 5000)
};


//时间戳转换为日期字符串 yyyy-MM-dd HH:mm:ss
function formatDateTime(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};

//时间戳转换为日期字符串 yyyy-MM-dd HH:mm:ss
function formatDateTime1(inputTime, time) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d + ' ' + time;
};

function nowInDateBetwen(d1, d2) {
    //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
    //var dateBegin = new Date(d1.replace(/-/g, "/"));//将-转化为/，使用new Date
    //  var dateEnd = new Date(d2.replace(/-/g, "/"));//将-转化为/，使用new Date
    //  var dateNow = new Date(date.replace(/-/g, "/"));//将-转化为/，使用new Date
    var dateBegin = new Date(d1); //将-转化为/，使用new Date
    var dateEnd = new Date(d2); //将-转化为/，使用new Date
    var dateNow = new Date(); //获取当前时间

    var beginDiff = dateNow.getTime() - dateBegin.getTime(); //时间差的毫秒数
    var beginDayDiff = Math.floor(beginDiff / (24 * 3600 * 1000)); //计算出相差天数

    var endDiff = dateEnd.getTime() - dateNow.getTime(); //时间差的毫秒数
    var endDayDiff = Math.floor(endDiff / (24 * 3600 * 1000)); //计算出相差天数
    if (endDayDiff < 0) { //已过期
        return false
    }
    if (beginDayDiff < 0) { //没到开始时间
        return false;
    }
    if (endDayDiff == 0) {
        return true
    }
    if (beginDayDiff == 0) {
        return true;
    }
    return true;
}
